package com.yuanqiworld.vw.domain;

import org.apache.commons.io.IOUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.roo.addon.dod.RooDataOnDemand;

import java.io.IOException;
import java.util.*;

@RooDataOnDemand(entity = UserInfo.class)
public class UserInfoDataOnDemand {

    public static void main(String[] args) throws IOException {
        // 启动前请将<property name="hibernate.hbm2ddl.auto" value="create"/>.
        new ClassPathXmlApplicationContext("classpath:/META-INF/spring/applicationContext.xml");
        initDB();
    }

    public static void initDB() throws IOException {
        RoleInfoDataOnDemand.initDB();

        String json = IOUtils.toString(UserInfoDataOnDemand.class.getClassLoader().getResource("UserInfo.json"));
        Collection<UserInfo> userInfos = UserInfo.fromJsonArrayToUserInfoes(json);
        for (UserInfo userInfo : userInfos) {
            Set<RoleInfo> roleInfos = new HashSet<RoleInfo>();
            for (RoleInfo roleInfo : userInfo.getRoles()) {
                 roleInfos.add(RoleInfo.findRoleInfo(roleInfo.getId()));
            }
            userInfo.setRoles(roleInfos);
            userInfo.encodePassword();
            userInfo.merge();
        }

    }
}
